package com.heima.kafka.sample;

/**
 * @author Gareth Cao
 * @date 2022/6/21 19:48
 */

import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.clients.producer.ProducerRecord;

import java.util.Properties;

/**
 * kafka生产者
 */
public class ProducerQuickStart {
    public static void main(String[] args) {
        //2 kafka的配置信息
        Properties properties = new Properties();
        //2.1 kafka的连接地址
        properties.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG,"192.168.178.133:9092");
        //2.2 kafka发送失败时的重复次数,自定义5次
        properties.put(ProducerConfig.RETRIES_CONFIG,5);
        //2.3 生产者消息key的序列化器
        properties.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG,"org.apache.kafka.common.serialization.StringSerializer");
        //2.4 生产者消息value的序列化器
        properties.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG,"org.apache.kafka.common.serialization.StringSerializer");
        //1 创建kafka生产者对象
        KafkaProducer<String,String>producer = new KafkaProducer<String,String>(properties);
        //3.1 封装消息
        ProducerRecord<String, String> record = new ProducerRecord<>("kafka","kafka_first","hello kafka");
        //3 发送消息
        producer.send(record);
        //4 释放资源
        producer.close();

    }
}
